package com.knoya.chejian.pda.module.pandian

import android.app.ActivityManager
import android.app.AlertDialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.View
import com.knoya.chejian.pda.R
import com.knoya.chejian.pda.activity.BaseActivity
import com.knoya.chejian.pda.minterface.OnUserDataInterface
import com.knoya.chejian.pda.myadapter.PandianAdapter
import com.knoya.chejian.pda.util.FileUtils
import com.knoya.chejian.pda.util.JsonUtil
import com.knoya.chejian.pda.util.Myutil
import kotlinx.android.synthetic.main.activity_pandian.*
import kotlinx.android.synthetic.main.header_knoya.*
import org.json.JSONException
import org.json.JSONObject

class PandianActivity : BaseActivity(), View.OnClickListener{

    var issaoyunxu = true
    var issao = false
    lateinit var pcodes : ArrayList<String>
    lateinit var pandianAdapter : PandianAdapter
    lateinit var M : PandianM
    lateinit var dialog: AlertDialog
    override fun getLayoutId(): Int {
        return R.layout.activity_pandian
    }

    override fun initCreate() {
        M = PandianM()
        pcodes = ArrayList()
        tv_header_title.text = "盘点"
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        Bt_Pandian_Ok.setOnClickListener(this)
        Bt_Qingkong.setOnClickListener(this)
        pandianAdapter = PandianAdapter(pcodes,this)
        Lv_pcode.adapter = pandianAdapter
        mBoradcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                if(!isAPPBroughtToBackground()){
                    return
                }
                if(!issaoyunxu){
                    return
                }
                if (intent.action == "nlscan.action.SCANNER_RESULT" && intent.hasExtra("SCAN_BARCODE1")) {
                    val scanResult_1 = intent.getStringExtra("SCAN_BARCODE1").trim()
                    samiaoOk(scanResult_1)
                }
                if(intent.action == "android.intent.ACTION_DECODE_DATA" && intent.hasExtra("barcode_string")){
                    val scanResult = intent.getStringExtra("barcode_string").trim()
                    samiaoOk(scanResult)
                }
            }
        }
    }

    override fun onClick(v: View?) {
        when(v){
            iv_header_left -> {
                finish()
            }

            Bt_Pandian_Ok -> {
                if(pcodes.size == 0){
                    Myutil.MyTost("扫描数据不能为空")
                    return
                }
                dialog = Myutil.showupdatedialog(this@PandianActivity, "正在上传", false)
                M.postPandian(this,pcodes,object :OnUserDataInterface{
                    override fun onUserData(data: String?) {
                        Log.d("TAAG",data)
                        var status = ""
                        var msg = ""
                        try {
                            var jo = JSONObject(data)
                            status = JsonUtil.getvolue(jo,"status")
                            msg = JsonUtil.getvolue(jo ,"msg")
                        }catch (e : JSONException){
                            e.printStackTrace()
                        }
                        if(msg != ""){
                            Myutil.tishidialog(this@PandianActivity,msg)
                        }
                        dialog.dismiss()
                        if(status != "success"){
                            return
                        }
                        pcodes.clear()
                        pandianAdapter.notifyDataSetChanged()
                        dialog.dismiss()
                    }

                    override fun onNot(throwable: String?) {
                        dialog.dismiss()
                    }

                })
            }
            Bt_Qingkong -> {
                pcodes.clear()
                pandianAdapter.notifyDataSetChanged()
            }
        }
    }

    fun samiaoOk(data : String){
        Log.d("TAAG",data);
        if(data.length>28){
            Myutil.tishidialog(this@PandianActivity,"条码扫描错误，请重新扫描")
            Myutil.zhendong(this)
            FileUtils.playSound(R.raw.baojing);
        }
        if(pcodes.contains(data)){
            Myutil.MyTost("重复扫描")
            return;
        }
        pcodes.add(data)
        pandianAdapter.notifyDataSetChanged()
    }

    fun isAPPBroughtToBackground(): Boolean {
        val am = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val tasks = am.getRunningTasks(1)
        if (!tasks.isEmpty()) {
            val topActivity = tasks[0].topActivity
            if (topActivity.className == this.javaClass.name) {

                return true
            }
        }
        return false
    }
}